<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row>
          <el-form-item>
            <el-input
              v-model="filters.number"
              placeholder="选择商家"
              style="width: 200px;"
              readonly
              @focus="showShop"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              readonly
              v-model="filters.number"
              placeholder="选择店铺名称"
              style="width: 200px;"
              @focus="showStore"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-input
              readonly
              v-model="filters.equivalencyMin"
              placeholder="选择供应商"
              style="width: 200px;"
              @focus="showSupplier"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-input v-model="filters.client" placeholder="收货单号"></el-input>
          </el-form-item>

          <el-form-item>
            <el-select v-model="filters.status" style="width: 200px;" placeholder="单据状态">
              <el-option label="待确认" value="0"></el-option>
              <el-option label="已审核" value="1"></el-option>
              <el-option label="处理中" value="2"></el-option>
              <el-option label="已完成" value="3"></el-option>
              <el-option label="已取消" value="3"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-select v-model="filters.status" style="width: 200px;" placeholder="收货状态">
              <el-option label="待收货" value="0"></el-option>
              <el-option label="待校验" value="1"></el-option>
              <el-option label="待入库" value="2"></el-option>
              <el-option label="部分入库" value="3"></el-option>
              <el-option label="已入库" value="3"></el-option>
              <el-option label="作废" value="3"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-select v-model="filters.status" style="width: 200px;" placeholder="交易币别">
              <el-option label="CNY" value="0"></el-option>
              <el-option label="AUD" value="1"></el-option>
              <el-option label="USD" value="2"></el-option>
              <el-option label="GBP" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="filters.createMin"
              type="date"
              placeholder="最早收货日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-date-picker
              v-model="filters.createMax"
              type="date"
              placeholder="最晚收货日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              style="width: 200px;"
            ></el-date-picker>
          </el-form-item>

          <el-row>
            <div style="float:right">
              <el-form-item>
                <kt-button
                  icon="fa fa-search"
                  :label="$t('action.search')"
                  type="primary"
                  @click="findPage(null)"
                />
              </el-form-item>
              <el-form-item>
                <kt-button
                  icon="fa fa-refresh" :label="$t('action.reset')"
                  type="primary"
                  @click="resetForm('filters')"
                />
              </el-form-item>

              <el-form-item>
                <kt-button label="导出" type="primary" @click="exInfo" />
              </el-form-item>
            </div>
          </el-row>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel" id="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      ref="CyTable"
      :data="pageResult"
      :columns="filterColumns"
      :showOperation="false"
      @selectionChange="selectionChange"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="所属商铺" prop="shop_id">
          <el-select v-model="dataForm.shop_id" placeholder="所属店铺" style="width: 100%;">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="等价金额" prop="money">
          <el-input type="number" v-model="dataForm.money" auto-complete="off" ></el-input>
        </el-form-item>
        <el-form-item label="需要达到的订单金额" prop="full_money">
          <el-input
            type="number"
            v-model="dataForm.full_money"
            auto-complete="off"
           
          ></el-input>
        </el-form-item>
        <el-form-item label="需要达到的订单数量" prop="full_nums">
          <el-input
            type="number"
            v-model="dataForm.full_nums"
            auto-complete="off"
           
          ></el-input>
        </el-form-item>

        <el-form-item label="有效开始时间" prop="start">
          <el-date-picker
            v-model="dataForm.starttime"
            style="width:100%"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
           
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="有效结束时间" prop="end">
          <el-date-picker
            v-model="dataForm.endtime"
            style="width:100%"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
           
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="false"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>

    <el-dialog title="指定商品页面" :visible.sync="checkpages" width="30%" height="100%">
      <el-form style="text-align:center">
        <el-row>
          <el-form-item>
            <span>选择范围:</span>
            <el-input
              type="number"
              v-model="dataForm.money"
              auto-complete="off"
          
            ></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="checkpaged">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="选择商家"
      :visible.sync="showModel"
      width="45%"
      height="100%"
      :model="dataForm"
      label-width="80px"
     
   
     
      label-position="right"
    >
      <el-form
        style="text-align:left"
        :model="dataForm"
        label-width="80px"
       
        label-position="right"
      >
        <el-row>
          <div style="text-align:center">
            <el-form-item label="商家编码" prop="money" style="float:left;margin-left:90px">
              <el-input v-model="dataForm.money" auto-complete="off" width="20%"></el-input>
            </el-form-item>

            <el-form-item label="商家名称" prop="money" style="float:left;margin-left:90px">
              <el-input v-model="dataForm.money" auto-complete="off" width="20%"></el-input>
            </el-form-item>
          </div>
        </el-row>

        <div style="text-align:center">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-refresh" :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </div>

        <el-table :data="tableData" border style="width: 100%" :visible.sync="showTable">
          <el-table-column prop="number" label="商家编码" width="280"></el-table-column>
          <el-table-column prop="name" label="商家名称" width="280"></el-table-column>
          <el-table-column prop="type" label="商家类型"></el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="cancels">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 店铺table -->
    <el-dialog
      title="选择店铺"
      :visible.sync="showModel1"
      width="45%"
      height="100%"
      :model="dataForm"
      label-width="80px"
     
      label-position="right"
    >
      <el-form
        style="text-align:left"
        :model="dataForm"
        label-width="80px"
       
        label-position="right"
      >
        <el-row>
          <div style="text-align:center">
            <el-form-item label="店铺编码" prop="money" style="float:left;margin-left:90px">
              <el-input v-model="dataForm.money" auto-complete="off" width="20%"></el-input>
            </el-form-item>

            <el-form-item label="店铺名称" prop="money" style="float:left;margin-left:90px">
              <el-input v-model="dataForm.money" auto-complete="off" width="20%"></el-input>
            </el-form-item>
          </div>
        </el-row>

        <div style="text-align:center">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-refresh" :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </div>

        <el-table :data="tableData1" border style="width: 100%" :visible.sync="showTable1">
          <el-table-column prop="checks" type="selection"></el-table-column>
          <el-table-column prop="name" label="商家名称" width="200"></el-table-column>
          <el-table-column prop="number" label="店铺编码" width="200"></el-table-column>
          <el-table-column prop="storename" label="店铺名称" width="200"></el-table-column>
          <el-table-column prop="storetype" label="店铺类型" width="200"></el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="cancels2">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 供应商table -->
    <el-dialog
      title="选择供应商"
      :visible.sync="showModel2"
      width="45%"
      height="100%"
      :model="dataForm"
      label-width="80px"
     
      label-position="right"
    >
      <el-form
        style="text-align:left"
        :model="dataForm"
        label-width="80px"
      
        label-position="right"
      >
        <el-row>
          <div style="text-align:center">
            <el-form-item label="供应商编码" prop="money" style="float:left;margin-left:90px">
              <el-input v-model="dataForm.money" auto-complete="off" width="20%"></el-input>
            </el-form-item>

            <el-form-item label="供应商名称" prop="money" style="float:left;margin-left:90px">
              <el-input v-model="dataForm.money" auto-complete="off" width="20%"></el-input>
            </el-form-item>
          </div>
        </el-row>

        <div style="text-align:center">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
            <kt-button
              icon="fa fa-refresh" :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
        </div>

        <el-table :data="tableData2" border style="width: 100%" :visible.sync="showTable2">
          <el-table-column prop="checks" type="selection"></el-table-column>
          <el-table-column prop="number" label="供应商编码" width="400"></el-table-column>
          <el-table-column prop="name" label="供应商名称" width="400"></el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="cancels3">取 消</el-button>
      </div>
    </el-dialog>

    <!--样式优化注释，后期需要时放开-->
     <!-- <div class="tab-container">
            <el-alert :closable="false" style="background-color: #ffffff;color: #ffffff;" title="" type="success" />
            <el-tabs v-model="activeName" style="margin-top:15px;" type="border-card">
              <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key">
                <keep-alive>
                  <tab-pane v-if="activeName==item.key" :type="item.key" />
                </keep-alive>
              </el-tab-pane>
            </el-tabs>
          </div> -->
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import { exportExcel } from "@/utils/excel";
export default {
  components: {
    PopupTreeInput,
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      activeName: '0',
      //商家弹窗表格内容
      tableData: [
        {
          number: "300001",
          name: "王小虎",
          type: "张小二服饰"
        },
        {
          number: "300002",
          name: "录屏商家",
          type: "入驻商家"
        },
        {
          number: "300003",
          name: "测试商家",
          type: "自营商家"
        },
        {
          number: "300004",
          name: "育学园",
          type: "入驻商家"
        }
      ],
      //店铺弹窗表格内容
      tableData1: [
        {
          name: "张小二服饰",
          number: "jd_001",
          storename: "京东店",
          storetype: "线上店铺"
        },
        {
          name: "张小二服饰",
          number: "tm_001",
          storename: "张小二的二号店",
          storetype: "线上店铺"
        },
        {
          name: "张小二服饰",
          number: "jd_002",
          storename: "育学园天猫旗舰店",
          storetype: "线上店铺"
        },
        {
          name: "张小二服饰",
          number: "tm_002",
          storename: "张小二的bbc店铺",
          storetype: "线上店铺"
        }
      ],

      //供应商表格内容
      tableData2: [
        {
          number: "jd_001",
          name: "张小二服饰"
        },
        {
          number: "jd_002",
          name: "小杨服饰"
        },
        {
          number: "jd_003",
          name: "童颜无忌"
        },
        {
          number: "tm_004",
          name: "爱尚"
        }
      ],

      size: "small",
      filters: {
        code: "",
        shopname: "",
        storecode: "",
        storename: "",
        suppliercode: "",
        suppliername: "",
        procode: "",
        pronum: "",
        proname: "",
        pronorm: "",
        protype: "",
        calculateunit: "",
        stockunit: "",
        conversionrate: "",
        startnums: "",
        endarea: "",
        effectivedate: "",
        expirationdate: "",
        moneytype: "",
        purchaseprice: "",
        taxrate: "",
        unpurchaseprice: "",
        agreementprice: "",
        unagreementprice: "",
        transporttype: "",
        isfreight: "",
        supplier: "",
        contracttype: "",
        contractcode: "",
        contractttributes: "",
        remark: "",
        res: {
          total: 1,
          code: "0000",
          rows: [
            {
             
              shopname: " 育学园-测试",
              storecode: " 005",
              storename: " 测试门店",
              receiveno: "SH1910080000209479",
              expirationdate: " 2019-10-08	",
              suppliercode: " 1910080000196567	",
              suppliername: " 测试供应商	",
              warehousename: " 测试仓库",
              moneytype: "USD",
              taxrate: "10.00%	",
              purchaseprice: " 40.00",
              orederstatus: "已审核",
              receivestatus: "已校验待入库",
              send_status: "等待发送	",
              return_status: "未回调	",
              creat_time: "2019-10-27",
              creator: "13875977921"
            }
          ]
        }
      },
      the_products: "",
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},
      options: {},
      showModel: false,
      showTable: false,
      showModel1: false,
      showTable1: false,
      showModel2: false,
      showTable2: false,
      checkpages: false,
      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
     

      // 新增编辑界面数据
      dataForm: {
        shop_id: "",
        money: "",
        full_money: "",
        full_nums: "",
        starttime: "",
        endtime: " "
      },
      selections: []
    };
  },
  methods: {
    //商家table
    showShop() {
      this.showModel = true;
      this.showTable = true;
    },
    //店铺table
    showStore() {
      this.showModel1 = true;
      this.showTable1 = true;
    },
    showSupplier() {
      this.showModel2 = true;
      this.showTable2 = true;
    },
    

    
    selectionChange: function(data) {
      this.selections = data.selections;
    },
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPageStaticState(this.filters);
    },

    // 批量删除
    handleDelete: function(data) {
      var ids = "";
      for (var i = 0; i < data.params.length; i++) {
        ids = ids + data.params[i].id + ",";
      }
      data.params.t = "marketCoupons";
      data.params.ids = ids;
      this.utils.request.deleteUserInfo(data.params, data.callback);
    },

    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true;
      this.operation = true;
      this.dataForm = {};
      console.log(dataForm);
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "优惠卷配置";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    // 显示编辑界面
    handleEdit: function(params) {
      console.log(params);
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
    },

    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            let params = Object.assign({}, this.dataForm);
            params.t = "marketCoupons";
            this.utils.request.saveMarketCoupon(params, this.saveSeckillBack);
          });
        }
      });
    },

    // 新增修改回调
    saveSeckillBack: function(data) {
      if (data.code == "0000") {
        this.$message({ message: "操作成功", type: "success" });
        this.dialogVisible = false;
        this.$refs["dataForm"].resetFields();
      } else {
        this.$message({ message: "操作失败, " + data.msg, type: "error" });
      }
      this.findPage({ t: "marketCoupons" });
      this.dialogVisible = false;
      this.operation = false;
      this.editLoading = false;
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "action", label: "操作", minWidth: 50 },

        { prop: "shopname", label: "商家名称", minWidth: 100 },
        { prop: "storecode", label: "店铺编码", minWidth: 100 },
        { prop: "storename", label: "店铺名称", minWidth: 100 },
        { prop: "receiveno", label: "收货单号", minWidth: 100,formatter:this.orderDetail},
        { prop: "expirationdate", label: "生效日期", minWidth: 100 },
        { prop: "suppliercode", label: "供应商编码", minWidth: 50 },
        { prop: "suppliername", label: "供应商名称", minWidth: 100 },
        { prop: "warehousename", label: "仓库名称", minWidth: 50 },
        { prop: "moneytype", label: "交易币别", minWidth: 50 },
        { prop: "taxrate", label: "汇率", minWidth: 50 },
        { prop: "purchaseprice", label: "含税金额", minWidth: 50 },
        { prop: "orederstatus", label: "单据状态", minWidth: 50 },
        { prop: "receivestatus", label: "收货状态", minWidth: 100 },
        { prop: "send_status", label: "下发wms状态", minWidth: 50 },
        { prop: "return_status", label: "wms回调状态", minWidth: 50 },
        { prop: "creat_time", label: "创建时间", minWidth: 50 },
        { prop: "creator", label: "制单人", minWidth: 50 }
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    resetForm(formName) {
      this.filters = {
        name: "",
        code: "",
        types: "",
        sortno: ""
      };
      this.findPage({ t: "marketCoupons" });
    },
    // 所属商品分类
    queryProType() {
      var that = this;
      this.utils.request.queryUserList({ t: "shop" }, function(data) {
        that.options = data.data;
      });
    },
    //启用停用按钮
    //启用
    startRule: function() {
      if (this.selections.length != 1) {
        this.$message({ message: "请选择一条数据", type: "error" });
        return;
      } else {
        var data = {};
        data.id = data.id = this.selections[0].id;
        data.t = "marketCoupons";
        data.status = 1;

        var this_ = this;
        this.utils.request.editUserInfo(data, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "启用成功", type: "success" });

            this_.findPage();
          } else {
            this_.$message({ message: res.msg, type: "error" });
          }
        });
      }
    },

    //停用按钮
    endRule: function() {
      if (this.selections.length != 1) {
        this.$message({ message: "请选择一条数据", type: "error" });
        return;
      } else {
        var data = {};
        data.id = this.selections[0].id;
        data.t = "marketCoupons";
        data.status = 0;

        var this_ = this;
        this.utils.request.editUserInfo(data, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "停用成功", type: "success" });

            this_.findPage();
          } else {
            this_.$message({ message: res.msg, type: "error" });
          }
        });
      }
    },
    //关联按钮
    relatedProducts: function() {
      if (this.selections.length != 1) {
        this.$message({ message: "请选择一条数据", type: "error" });
        this.checkpages = false;
        return;
      } else {
        this.checkpages = true;
      }
    },
    //指定商品
    checkpaged: function() {
      var data = {};
      data.id = this.selections[0].id;
      data.t = "marketCoupons";
      data.the_products = this.the_products;
      var this_ = this;
      if (" " != this.the_products && this.the_products == 2) {
        this.$router.push({
          path: "/product/productEntry",
          query: { couponid: this.selections[0].id }
        });
      } else {
        this.utils.request.editUserInfo(data, function(res) {
          if (res.code == "0000") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.checkpages = false;
            this_.findPage();
          } else {
            this_.$message({ message: res.msg, type: "error" });
          }
        });
      }
    },
    //导出按钮
    exInfo: function() {
      this.$message({ message: "导出成功", type: "success" });
    },
    //取消按钮
    cancels: function() {
      this.showModel = false;
      this.showTable = false;
    },
    cancels2: function() {
      this.showModel1 = false;
      this.showTable1 = false;
    },
    cancels3: function() {
      this.showModel2 = false;
      this.showTable2 = false;
    },
    cancel: function() {
      this.selections[0].id = " ";
      var this_ = this;
      this_.checkpages = false;
      this_.findPage();
    },
    //订单号跳转
    orderDetail(row, column, cellValue, index){
       return '<span @click ="jump" class="orderJump">'+'<font color="blue">'+cellValue+'</font>'+'</span>'

    }
  },
 watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    }
  },  
  created() {
    // init the default selected tab
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
  mounted() {
    this.initColumns();
    this.queryProType();
    $(document).on(
      "click",
      ".orderJump",
       function(e) {
          this.$router.push("/UnReceiptDetail");
      }.bind(this)
     
    );
  }
};
</script>

<style scoped>
/deep/.orderJump{
  
   color:blue;
   
 }
</style>